package com.yjhb.android.sbz.dialog;

import android.app.Dialog;
import android.content.Context;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.yjhb.android.sbz.R;
import com.yjhb.android.sbz.tool.EventMsg;
import com.yjhb.android.sbz.entity.TaskStep;
import com.yjhb.android.sbz.tool.Tool;
import com.jojo.android.zxlib.tool.ZxToast;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;
import java.util.UUID;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 创建者 zx
 * 创建时间 2020/11/25 16:42
 */
public class PublishStepDialog extends Dialog implements View.OnClickListener {
    private static final int REQUEST_IMAGE = 8001;
    private final String TAG = "PublishStepDialog";

    @BindView(R.id.title_tv)
    TextView title_tv;
    @BindView(R.id.stepDire_et)
    EditText stepDire_et;
    @BindView(R.id.text_layout)
    LinearLayout text_layout;
    @BindView(R.id.text_tv)
    TextView text_tv;
    @BindView(R.id.text_et)
    EditText text_et;
    @BindView(R.id.image_tv)
    TextView image_tv;
    @BindView(R.id.image_iv)
    ImageView image_iv;
    @BindView(R.id.image2_tv)
    TextView image2_tv;
    @BindView(R.id.image_layout)
    LinearLayout image_layout;
    @BindView(R.id.cancel_tv)
    TextView cancel_tv;
    @BindView(R.id.submit_tv)
    TextView submit_tv;

    private String imgPath;
    private TaskStep taskStep;

    /**
     * 步骤类型:1网址2二维码3图片4文字5复制6上传截图7收集信息
     */
    int type;
    private OnSubmitListener onSubmitListener;

    public PublishStepDialog(@NonNull Context context, int type) {
        super(context, R.style.mDialog);
        this.type = type;
        init();
    }

    private PublishStepDialog(@NonNull Context context) {
        super(context);
    }

    private PublishStepDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }


    private void init() {
        View view = LayoutInflater.from(getContext()).inflate(R.layout.dialog_pulish_step, null, false);
        setContentView(view);
        setCancelable(true);
        setCanceledOnTouchOutside(false);
        ButterKnife.bind(this);
        cancel_tv.setOnClickListener(this);
        submit_tv.setOnClickListener(this);
        image2_tv.setOnClickListener(this);

    }

    @Override
    protected void onStart() {
        EventBus.getDefault().register(this);
        super.onStart();
    }

    @Override
    protected void onStop() {
        EventBus.getDefault().unregister(this);
        super.onStop();
    }

    private void initUI() {
        switch (type) {
            case 1:
                title_tv.setText("输入网址");
                stepDire_et.setHint("该步骤类型适用于需要点击链接，访问网页的操作。输入内容，说明打开网址的相关操作以及注意事项。");
                text_layout.setVisibility(View.VISIBLE);
                text_tv.setText("填写网址:");
                text_et.setHint("网址在此处填写");
                image_layout.setVisibility(View.GONE);
                break;
            case 2:
                title_tv.setText("传二维码");
                stepDire_et.setHint("该步骤类型适用于需要扫二维码的操作。输入内容，详细说明扫码的相关操作以及注意事项。");
                text_layout.setVisibility(View.GONE);
                image_layout.setVisibility(View.VISIBLE);
                image_tv.setText("二维码图:");

                break;
            case 3:
                title_tv.setText("图文说明");
                stepDire_et.setHint("图文说明通过示例图的方式引导接悬赏的人进行操作，在此填写步骤描述，详细说明该步骤的操作方式、注意事项等。");
                text_layout.setVisibility(View.GONE);
                image_layout.setVisibility(View.VISIBLE);
                image_tv.setText("图片说明:");
                break;
            case 4:
                title_tv.setText("文字说明");
                stepDire_et.setHint("适用于纯文字说明，可在此简要介绍任务情况，任务备注等。");
                text_layout.setVisibility(View.GONE);
                image_layout.setVisibility(View.GONE);
                break;
            case 5:
                title_tv.setText("复制数据");
                stepDire_et.setHint("适用于需要复制数据的场景，如填写邀请码，在此填写内容，方便用户复制数据。");
                text_layout.setVisibility(View.VISIBLE);
                text_tv.setText("填写数据:");
                text_et.setHint("如邀请码，钱包地址等");
                image_layout.setVisibility(View.GONE);
                break;
            case 6:
                title_tv.setText("收集截图");
                stepDire_et.setHint("适用于收集截图结果。输入内容，详细描述需要截图哪个界面，需要满足什么要求等。在下方上传示例图演示。");
                text_layout.setVisibility(View.GONE);
                image_layout.setVisibility(View.VISIBLE);
                image_tv.setText("演示例图:");
                break;
            case 7:
                title_tv.setText("收集信息");
                stepDire_et.setHint("适用于收集手机号、姓名、用户名、ID等，在此说明您需要收集的信息。");
                text_layout.setVisibility(View.GONE);
                image_layout.setVisibility(View.GONE);
                break;
        }
        if (taskStep != null) {
            stepDire_et.setText(taskStep.getTitle());
            switch (type) {
                case 1:
                case 5:
                    text_et.setText(taskStep.getContent());
                    break;
                case 2:
                case 3:
                case 6:
                    imgPath = taskStep.getContent();
                    break;
            }
        }
        upImage();
    }

    private void upImage() {
        if (!TextUtils.isEmpty(imgPath)) {
            image_iv.setVisibility(View.VISIBLE);
            Glide.with(getContext()).load(imgPath).into(image_iv);
        } else {
            image_iv.setVisibility(View.GONE);
        }
    }

    @Override
    public void dismiss() {
        super.dismiss();
        //销毁数据
        stepDire_et.setText("");
        text_et.setText("");
        imgPath = null;
    }

    @Override
    public void show() {
        show(4);
    }

    public void show(int type) {
        show(type, null);
    }

    public void show(int type, TaskStep step) {
        this.type = type;
        taskStep = step;
        initUI();
        super.show();
    }

    public void setType(int type) {
        this.type = type;
    }

    public void setOnSubmitListener(OnSubmitListener onSubmitListener) {
        this.onSubmitListener = onSubmitListener;
    }

    @Override
    public void onClick(View v) {
        if (v == cancel_tv) {
            dismiss();
        } else if (v == submit_tv) {
            TaskStep taskStep = createStep();
            if (taskStep == null) return;
            if (onSubmitListener != null) {
                onSubmitListener.onTaskStep(taskStep);
            }
            dismiss();
        } else if (v == image2_tv) {
            EventBus.getDefault().post(new EventMsg("request_select_img", null));
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMsg(EventMsg msg) {
        if (msg.msg != null && msg.msg.equals("image_selected")) {
            List<String> paths = (List<String>) msg.object;
            imgPath = paths.get(0);
            upImage();
        }
    }


    /**
     * 生成任务
     */
    private TaskStep createStep() {
        String stepContent = "";
        if (taskStep == null) {
            taskStep = new TaskStep();
            taskStep.setsId(UUID.randomUUID().toString().replaceAll("-",""));
        }
        if (Tool.isEditEmpty(stepDire_et)) {
            ZxToast.showBottomToastLight("您没有填写步骤说明。");
            return null;
        }
        String stepDescription = stepDire_et.getText().toString();

        switch (type) {
            case 1:
            case 5:
                if (Tool.isEditEmpty(text_et)) {
                    ZxToast.showBottomToastLight("请将信息填写完整后确定。");
                    return null;
                }
                stepContent = text_et.getText().toString();
                break;
            case 2:
            case 3:
            case 6:
                if (TextUtils.isEmpty(imgPath)) {
                    ZxToast.showBottomToastLight("请选择一张图片。");
                    return null;
                }
                stepContent = imgPath;
                break;
        }
        taskStep.setTitle(stepDescription);
        taskStep.setContent(stepContent);
        taskStep.setType(type);
        return taskStep;
    }

    public interface OnSubmitListener {

        void onTaskStep(TaskStep step);

    }

}
